<?php
	require_once("../../include_dao.php");
		
	if(	$_POST['name'] != '' and $_POST['user'] != '' and 
		$_POST['pass'] != '' and $_POST['confpass'] != '' and 
		$_POST['meanscontact'] != '' and $_POST['meansidentification'] != '' ){
			
		$name = $_POST['name'];
		$user = $_POST['user'];
		$pass = $_POST['pass'];
		$birthdate = $_POST['birthdate'];
		$confpass = $_POST['confpass'];
		$meanscontact = $_POST['meanscontact'];
		$meansidentification = $_POST['meansidentification'];
		
		if($confpass == $pass){
			// Inicia la Trasaccionalidad para hacer ingresos de informacion
			$transaction = new Transaction();			
			try{

				// Preparo para Guardar informacion Padre (Partner)
				$dtoPartner = new Partner();			
				$dtoPartner->fKTargetTypePerson = 2; // Indica que es un Cliente
				$dtoPartner->name = $name;
				$dtoPartner->birthdate = $birthdate;
				$dtoPartner->meansContact = $meanscontact;
				$dtoPartner->meansIdentification = $meansidentification;
				$dtoPartner->gender = 1;
				$dtoPartner->active = 1;
				$dtoPartner->visible = 1;
				/// Guarda La informacion del Partner 
				DAOFactory::getPartnerDAO()->insert($dtoPartner);
				
				// Preparo para Guardar informacion hijo (User)
				$dtoUser = new User();					
				$dtoUser->fKPartner = $dtoPartner->pKPartner;			
				$dtoUser->user = $user;			
				$dtoUser->pass = $pass;			
				$dtoUser->active = 1;			
				$dtoUser->visible = 1;
				/// Guarda La informacion del User 
				DAOFactory::getUserDAO()->insert($dtoUser);
				
				// Almaceno la informacion del usuario registrado 
				session_start();
				$_SESSION['dtoUser']	= serialize ($dtoUser);
				$_SESSION['dtoPartner']	= serialize ($dtoPartner);
				
				//commit transaction
				$transaction->commit();
				
				// Redirecciono a la pantalla principal 
				header( 'Location: ../../partner_list.php');
			}catch (Exception $e){	
			
				// Anulo la Transaccion
				$transaction->rollback();			
				header( 'Location: ../../register.php?err=31');
			}	
		}else{
			header( 'Location: ../../register.php?err=12');
		}
		
	}else{
		header( 'Location: ../../register.php?err=13');
	}
?>